package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.health.pojo.*;
import com.itcast.service.UserDataService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author LiHaoPeng
 * @date 2019-12-03 11:27
 */
@RestController
@RequestMapping("/userData")
public class UserDataController {
    @Reference
    private UserDataService userDataService;

    @RequestMapping("/add")
    public Result add(@RequestBody User user) {
        try {
             userDataService.add(user);
            return new Result(true, "新增用户信息成功");
        } catch (Exception e) {
            return new Result(false, "新增用户信息失败,请查看用户姓名是否存在");
        }

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userDataService.findPage(queryPageBean);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody User user) {
        try {
            userDataService.edit(user);
            return new Result(true, "编辑用户信息成功");
        } catch (Exception e) {
            return new Result(false, "编辑用户信息失败");
        }
    }

    @RequestMapping("/findRoles")
    public Result findRoles() {
        try {
            Set<Role> roles = userDataService.findRoles();
            return new Result(true,"",roles);
        } catch (Exception e) {
            return new Result(false, "");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userDataService.delete(id);
            return new Result(true, "删除用户成");
        } catch (Exception e) {
            return new Result(false, "删除用户失败");
        }

    }

    @RequestMapping("/findById4Edit")
    public Result findById4Edit(Integer id) {
        try {
            Map map = userDataService.findById4Edit(id);
            return new Result(true, "",map);
        } catch (Exception e) {
            return new Result(false, "");
        }
    }
}
